/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ut__frstat_do_related.c                           */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/30 by guojq                                */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "frstat_server.h"
#include "apmtest_cunit.h"

#define SERVER_STAT_HASH_SIZE    200
extern int g_i_flag_run;
extern unsigned long g_ul_pcap_queue_used;

typedef struct __conn_info {
    unsigned long ul_connections;
    unsigned long ul_requests;
    unsigned long ul_responses;
} conn_info;

typedef struct __conn_avg {
    unsigned long ul_connections[4];
    unsigned long ul_requests[4];
    unsigned long ul_responses[4];
} conn_avg;

typedef struct __server_info {
    u_int   ui_ip;
    u_short us_port;
    int     i_l7_proto;

    struct timeval start_time;
    conn_info st_conns_total;
    conn_avg  st_conns_avg;
} Server_info;

//端口关联表
typedef struct __port_related {
    u_int   ui_ip;
    u_short us_port;
    u_int   ui_ip_related;
    u_short us_port_related;

    struct timeval start_time;
} Port_related;

//做完关联后的hash节点
typedef struct __server_info_avg {
    u_int   ui_ip;
    u_short us_port;
    int     i_l7_proto;

    unsigned long ul_connections;
    unsigned long ul_requests;
    unsigned long ul_responses;

    unsigned long ul_connections_avg;
    unsigned long ul_requests_avg;
    unsigned long ul_responses_avg;
} Server_info_avg;


void __server_node_free(Frlist_node* p_node);
extern int __frstat_server_output(Frlist_node* p_node, const void *p_data);
void __outhash_node_free(Frlist_node* p_node);
int __frstat_outhash_add(Frhash_header *p_st_hash, u_int ui_ip, u_short us_port, int i_l7_proto
                                , unsigned long ul_connections, unsigned long ul_requests, unsigned long ul_responses
                                , unsigned long ul_connections_avg, unsigned long ul_requests_avg, unsigned long ul_responses_avg);

int __frstat_do_related(Frlist_node* p_node, const void *p_data);

static int __suite_init(void)
{
    g_i_flag_run = 1;
    g_ul_pcap_queue_used = 0L;
    return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

static void case1(void)
{
    Frlist_node *p_st_node = NULL;
    Frhash_header st_out;
    int i_ret = 0;
    char *p_outbuff = NULL;
    Server_info *p_server_info = NULL;
    
    frhash2_create(&st_out, SERVER_STAT_HASH_SIZE);

    p_server_info = (Server_info *)frmalloc(sizeof(Server_info));
    
    p_server_info->ui_ip = 16781484;
    p_server_info->us_port = 80;
    p_server_info->i_l7_proto = 1433;
    p_server_info->st_conns_total.ul_connections = 300;
    p_server_info->st_conns_total.ul_requests = 200;
    p_server_info->st_conns_total.ul_responses = 150;
    
    p_server_info->st_conns_avg.ul_connections[0] = 250;
    p_server_info->st_conns_avg.ul_requests[0] = 150;
    p_server_info->st_conns_avg.ul_responses[0] = 100;
    p_server_info->st_conns_avg.ul_connections[1] = 220;
    p_server_info->st_conns_avg.ul_requests[1] = 120;
    p_server_info->st_conns_avg.ul_responses[1] = 80;
    p_server_info->st_conns_avg.ul_connections[2] = 200;
    p_server_info->st_conns_avg.ul_requests[2] = 100;
    p_server_info->st_conns_avg.ul_responses[2] = 70;
    p_server_info->st_conns_avg.ul_connections[3] = 160;
    p_server_info->st_conns_avg.ul_requests[3] = 90;
    p_server_info->st_conns_avg.ul_responses[3] = 40;
    
    p_st_node = frlist_node_create(p_server_info);
    
    __frstat_do_related(p_st_node, &st_out);
    

    frhash2_run(&st_out, &p_outbuff, __frstat_server_output);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    
    printf("\nout=[%s]\n", p_outbuff);

    __server_node_free(p_st_node);
    frfree(p_st_node);
    
    frhash2_free(&st_out, __outhash_node_free);
    
    frfree(p_outbuff);

    return;

}

static void case2(void)
{
    Frlist_node *p_st_node = NULL;
    Frhash_header st_out;
    int i_ret = 0;
    char *p_outbuff = NULL;
    Server_info *p_server_info = NULL;
    
    frstat_server_init(1);
    
    frhash2_create(&st_out, SERVER_STAT_HASH_SIZE);

    p_server_info = (Server_info *)frmalloc(sizeof(Server_info));
    
    p_server_info->ui_ip = 16781484;
    p_server_info->us_port = 80;
    p_server_info->i_l7_proto = 1433;
    p_server_info->st_conns_total.ul_connections = 300;
    p_server_info->st_conns_total.ul_requests = 200;
    p_server_info->st_conns_total.ul_responses = 150;
    
    p_server_info->st_conns_avg.ul_connections[0] = 250;
    p_server_info->st_conns_avg.ul_requests[0] = 150;
    p_server_info->st_conns_avg.ul_responses[0] = 100;
    p_server_info->st_conns_avg.ul_connections[1] = 220;
    p_server_info->st_conns_avg.ul_requests[1] = 120;
    p_server_info->st_conns_avg.ul_responses[1] = 80;
    p_server_info->st_conns_avg.ul_connections[2] = 200;
    p_server_info->st_conns_avg.ul_requests[2] = 100;
    p_server_info->st_conns_avg.ul_responses[2] = 70;
    p_server_info->st_conns_avg.ul_connections[3] = 160;
    p_server_info->st_conns_avg.ul_requests[3] = 90;
    p_server_info->st_conns_avg.ul_responses[3] = 40;
    
    p_st_node = frlist_node_create(p_server_info);
    
    frstat_server_related_add(16781484, 80, 83890348, 8000);
    
    __frstat_do_related(p_st_node, &st_out);
    

    frhash2_run(&st_out, &p_outbuff, __frstat_server_output);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    
    printf("\nout=[%s]\n", p_outbuff);

    frhash2_free(&st_out, __outhash_node_free);
    __server_node_free(p_st_node);
    frfree(p_st_node);
    
    frstat_server_destory();
    
    frfree(p_outbuff);

    return;

}

static void case3(void)
{
    Frlist_node *p_st_node = NULL;
    Frhash_header st_out;
    int i_ret = 0;
    char *p_outbuff = NULL;
    Server_info *p_server_info = NULL;
    
    frstat_server_init(1);
    
    frhash2_create(&st_out, SERVER_STAT_HASH_SIZE);

    p_server_info = (Server_info *)frmalloc(sizeof(Server_info));
    
    p_server_info->ui_ip = 16781484;
    p_server_info->us_port = 80;
    p_server_info->i_l7_proto = 1433;
    p_server_info->st_conns_total.ul_connections = 300;
    p_server_info->st_conns_total.ul_requests = 200;
    p_server_info->st_conns_total.ul_responses = 150;
    
    p_server_info->st_conns_avg.ul_connections[0] = 250;
    p_server_info->st_conns_avg.ul_requests[0] = 150;
    p_server_info->st_conns_avg.ul_responses[0] = 100;
    p_server_info->st_conns_avg.ul_connections[1] = 220;
    p_server_info->st_conns_avg.ul_requests[1] = 120;
    p_server_info->st_conns_avg.ul_responses[1] = 80;
    p_server_info->st_conns_avg.ul_connections[2] = 200;
    p_server_info->st_conns_avg.ul_requests[2] = 100;
    p_server_info->st_conns_avg.ul_responses[2] = 70;
    p_server_info->st_conns_avg.ul_connections[3] = 160;
    p_server_info->st_conns_avg.ul_requests[3] = 90;
    p_server_info->st_conns_avg.ul_responses[3] = 40;
    
    p_st_node = frlist_node_create(p_server_info);
    
    frstat_server_related_add(16781484, 8000, 83890348, 8000);
    frstat_server_related_add(83890348, 80, 83890348, 8000);
    
    __frstat_do_related(p_st_node, &st_out);
    

    frhash2_run(&st_out, &p_outbuff, __frstat_server_output);

    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    
    printf("\nout=[%s]\n", p_outbuff);

    frhash2_free(&st_out, __outhash_node_free);
    __server_node_free(p_st_node);
    frfree(p_st_node);
    
    frstat_server_destory();
    
    frfree(p_outbuff);

    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("ut__frstat_do_related 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);
    CU_add_test(pSuite, "case3", case3);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();
    return 0;
}